package com.jason.fingerprint.logic;
/**
 * @Title: RegisterFingerprintSyncLogic.java
 * @Package: com.jason.fingerprint.logic
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月24日 下午9:38:14
 * @Version: V1.0
 */

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.fgtit.fpcore.FPMatch;
import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.R;
import com.jason.fingerprint.common.UIHelper;

import android_serialport_api.AsyncFingerprint;
import android_serialport_api.AsyncFingerprint.OnDeleteCharListener;
import android_serialport_api.AsyncFingerprint.OnEmptyListener;
import android_serialport_api.AsyncFingerprint.OnLoadCharListener;
import android_serialport_api.AsyncFingerprint.OnRegModelListener;
import android_serialport_api.AsyncFingerprint.OnSearchListener;
import android_serialport_api.AsyncFingerprint.OnStoreCharListener;
import android_serialport_api.AsyncFingerprint.OnUpCharListener;
import android_serialport_api.SerialPortManager;

/**
 * @ClassName: RegisterFingerprintSyncLogic
 * @Description: 注册指纹
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月24日 下午9:38:14
 */
@SuppressLint("LongLogTag")
public class RegisterFingerprintSyncLogic extends Logic {

	private static final String TAG = "RegisterFingerprintSyncLogic";

	private AsyncFingerprint mRegisterFingerprint;
	private Handler mHandler;
	private boolean mIsCallBack = false;// 是否有回调，此需要Handle
	private boolean mIsShowImage = true;//是否显示图像
	private int mCount;
	private Message mMessage;

	private int mRetryCount = 0;

	private static boolean isRunning = false;

	public RegisterFingerprintSyncLogic() {

	}

	public RegisterFingerprintSyncLogic(AppContext appContext, Handler handler) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
		initSerialPort();
	}

	public RegisterFingerprintSyncLogic(AppContext appContext, Handler handler,
										boolean isCallBack) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
		this.mIsCallBack = isCallBack;
		initSerialPort();
	}

	public RegisterFingerprintSyncLogic(AppContext appContext, Handler handler,
										boolean isCallBack,boolean isShowImage) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
		this.mIsCallBack = isCallBack;
		this.mIsShowImage = isShowImage;
		initSerialPort();
	}

	//初始化注册信息
	private void initSerialPort() {
		try {
			if (!SerialPortManager.getInstance().isOpen()) {
				int code = FPMatch.getInstance().InitMatch(1, "http://www.hfcctv.com/");
				if (code == 0) {
					android.util.Log.i(TAG, "Init Match Error");
				} else {
					android.util.Log.i(TAG, "Init Match Success");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			android.util.Log.e(TAG, "Init Match Error", e);
		}
	}

	@Override
	public void execute() {
		initListener();
		registerFingerprintSync();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.jason.fingerprint.logic.Logic#send()
	 */
	@Override
	public void send() {
		if (mIsCallBack) {
			mHandler.sendMessage(mMessage);
			mMessage = new Message();
		}

	}

	//初始化监听
	private void initListener(){
		mRegisterFingerprint = SerialPortManager.getInstance().getNewAsyncFingerprint();
		if (!SerialPortManager.getInstance().isOpen()) {
			android.util.Log.i(TAG, "isRunning: " + isRunning);
			android.util.Log.i(TAG, "Open Error ");
			if (isRunning) {
				mMessage.what = UIHelper.FINGERPRINT_FAIL;
				Log.i(TAG, "---->setOnGetImageExListener onGetImageExFail 003");
				send();
				return;
			}
			return;
		} else {
			android.util.Log.i(TAG, "is Open: ");
		}
		mCount = 1;
		//判断返回时图片还是文字
		mRegisterFingerprint.setOnGetImageListener(new AsyncFingerprint.OnGetImageListener() {
			@SuppressLint("LongLogTag")
			@Override
			public void onGetImageSuccess() {
				Log.i(TAG, "---->setOnGetImageListener onGetImageSuccess 001");
				//cancleProgressDialog();
				if(mIsShowImage){
					mRegisterFingerprint.FP_UpImage();//
					//showProgressDialog("正在处理...");
					Log.i(TAG, "---->setOnGetImageListener onGetImageSuccess 008");
				}else{
					mRegisterFingerprint.FP_GenChar(mCount);
					Log.i(TAG, "---->setOnGetImageListener onGetImageSuccess 009");
				}
			}

			@Override
			public void onGetImageFail() {
				Log.i(TAG, "---->setOnGetImageListener onGetImageFail 002");
				mRetryCount ++;
				if (mRetryCount > 10){
					mMessage.what = UIHelper.FINGERPRINT_UPIMAGE_FAIL;
					send();
				}else {
					mRegisterFingerprint.FP_GetImage();
				}
			}
		});

		//图片---返回的图片结果
		mRegisterFingerprint.setOnUpImageListener(new AsyncFingerprint.OnUpImageListener() {
			@Override
			public void onUpImageSuccess(byte[] data) {
				Log.i(TAG, "---->setOnGetImageExListener onUpImageExSuccess 010");
				Log.i(TAG, "---->setOnUpImageExListener up image data.length="+data.length);
				mRegisterFingerprint.FP_GenChar(mCount);
				mMessage.what = UIHelper.FINGERPRINT_UPIMAGE_SUCCESS;
				Bundle bundle = new Bundle();
				bundle.putByteArray(UIHelper.FINGERPRINT_SOURCE, data);
				mMessage.setData(bundle);
				send();
			}

			@Override
			public void onUpImageFail() {
				Log.i(TAG, "---->setOnGetImageExListener onUpImageExFail 011");
				mMessage.what = UIHelper.FINGERPRINT_UPIMAGE_FAIL;
				send();
			}
		});

		//文字---监听文字返回结果
		mRegisterFingerprint.setOnGenCharListener(new AsyncFingerprint.OnGenCharListener() {
			@Override
			public void onGenCharSuccess(int bufferId) {
				Log.i(TAG, "---->setOnGenCharExListener onGenCharExSuccess 012 bufferId:" + bufferId);
				if (bufferId == 1) {
					//cancleProgressDialog();
					//showProgressDialog("请再按一次指纹！");
					mRegisterFingerprint.FP_GetImage();
					mCount++;
					mMessage.what = UIHelper.FINGERPRINT_AGAIN;
					send();
				} else if (bufferId == 2) {
					mRegisterFingerprint.FP_RegModel();
				}
			}

			@Override
			public void onGenCharFail() {
				Log.i(TAG, "---->setOnGenCharExListener onGenCharExFail 013");
				//cancleProgressDialog();
				//ToastUtil.showToast(FingerprintActivity.this,"生成特征值失败！");
				mMessage.what = UIHelper.FINGERPRINT_GENCHAR_FAIL;
				send();
			}
		});

		//文字---文字模板合成
		mRegisterFingerprint.setOnRegModelListener(new OnRegModelListener() {

			@Override
			public void onRegModelSuccess() {
				Log.i(TAG, "---->setOnGenCharExListener onGenCharExSuccess 014");
				//cancleProgressDialog();
				mRegisterFingerprint.FP_UpChar();
				//ToastUtil.showToast(FingerprintActivity.this, "合成模板成功！");
			}

			@Override
			public void onRegModelFail() {
				Log.i(TAG, "---->setOnGenCharExListener onGenCharExSuccess 015");
				//cancleProgressDialog();
				//ToastUtil.showToast(FingerprintActivity.this, "合成模板失败！");
				mMessage.what = UIHelper.FINGERPRINT_REGMODE_FAIL;
				send();
			}
		});

		//文字---返回文字结果集
		mRegisterFingerprint.setOnUpCharListener(new OnUpCharListener() {
			@Override
			public void onUpCharSuccess(byte[] model) {
				Log.i(TAG, "---->setOnGenCharExListener onGenCharExSuccess 016 lenth:" + model.length);
				//cancleProgressDialog();
				//Log.i("whw", "#################model.length="+model.length);
				//FingerprintActivity.this.model = model;
				//ToastUtil.showToast(FingerprintActivity.this, "注册成功！");
				if (model != null){
					Log.i(TAG, "---->onUpCharSuccess onGenCharExSuccess length:" + model.length);
					byte[] fpenrol=new byte[model.length];
					System.arraycopy(model, 0, fpenrol, 0, model.length);
					mMessage.what = UIHelper.FINGERPRINT_UPCHAR_SUCCESS;
					//Log.i(TAG, "---->setOnGenCharExListener onGenCharExSuccess 016 model:" + model);
					Bundle bundle = new Bundle();
					bundle.putByteArray(UIHelper.FINGERPRINT_SOURCE, fpenrol);
					mMessage.setData(bundle);
					send();
				}else{
					mMessage.what = UIHelper.FINGERPRINT_UPCHAR_FAIL;
					send();
				}
			}

			@SuppressLint("LongLogTag")
			@Override
			public void onUpCharFail() {
				Log.i(TAG, "---->setOnGenCharExListener onGenCharExSuccess 017");
				//cancleProgressDialog();
				//ToastUtil.showToast(FingerprintActivity.this, "注册失败！");
				mMessage.what = UIHelper.FINGERPRINT_UPCHAR_FAIL;
				send();
			}
		});

		//储存模板
		mRegisterFingerprint.setOnStoreCharListener(new OnStoreCharListener() {
			@Override
			public void onStoreCharSuccess() {
				//ToastUtil.showToast(FingerprintActivity.this, "储存模板成功！");
				Log.i(TAG, "---->setOnGenCharExListener onStoreCharSuccess 018");
				mMessage.what = UIHelper.FINGERPRINT_ONSTORECHAR_SUCCESS;
			}

			@Override
			public void onStoreCharFail() {
				//ToastUtil.showToast(FingerprintActivity.this, "储存模板失败！");
				Log.i(TAG, "---->setOnGenCharExListener onStoreCharFail 019");
				mMessage.what = UIHelper.FINGERPRINT_ONSTORECHAR_FAIL;
			}
		});

		//加载模板
		mRegisterFingerprint.setOnLoadCharListener(new OnLoadCharListener() {
			@Override
			public void onLoadCharSuccess() {
				//ToastUtil.showToast(FingerprintActivity.this, "加载模板成功！");
				mMessage.what = UIHelper.FINGERPRINT_LOADCHAR_SUCCESS;
				Log.i(TAG, "---->setOnGenCharExListener onLoadCharSuccess " + 20);
			}

			@Override
			public void onLoadCharFail() {
				//ToastUtil.showToast(FingerprintActivity.this, "加载模板失败！");
				mMessage.what = UIHelper.FINGERPRINT_LOADCHAR_FAIL;
				Log.i(TAG, "---->setOnGenCharExListener onLoadCharFail " + 21);
			}
		});

		//搜索指纹
		mRegisterFingerprint.setOnSearchListener(new OnSearchListener() {
			@Override
			public void onSearchSuccess(int pageId, int matchScore) {
				//ToastUtil.showToast(FingerprintActivity.this, "搜索成功！");
				Log.i("whw", "pageId=" + pageId + "    matchScore="	+ matchScore);
				mMessage.what = UIHelper.FINGERPRINT_SEARCH_SUCCESS;
				Log.i(TAG, "---->setOnGenCharExListener setOnSearchListener " + 22);
			}

			@Override
			public void onSearchFail() {
				//ToastUtil.showToast(FingerprintActivity.this, "搜索失败！");
				mMessage.what = UIHelper.FINGERPRINT_SEARCH_FAIL;
				Log.i(TAG, "---->setOnGenCharExListener onSearchFail " + 23);
			}
		});

		//删除制定的指纹
		mRegisterFingerprint.setOnDeleteCharListener(new OnDeleteCharListener() {
			@Override
			public void onDeleteCharSuccess() {
				Log.i(TAG, "---->setOnDeleteCharListener onSearchFail " + 24);
				mMessage.what = UIHelper.FINGERPRINT_DELETECHAR_SUCCESS;
			}

			@Override
			public void onDeleteCharFail() {
				Log.i(TAG, "---->onDeleteCharFail onSearchFail " + 25);
				mMessage.what = UIHelper.FINGERPRINT_DELETECHAR_FAIL;
			}
		});

		//清空数据
		mRegisterFingerprint.setOnEmptyListener(new OnEmptyListener() {
			@Override
			public void onEmptySuccess() {
				Log.i(TAG, "---->onDeleteCharFail onEmptySuccess " + 26);
				//Log.i("whw", "清空成功！");
				mMessage.what = UIHelper.FINGERPRINT_EMPTY_SUCCESS;
			}

			@Override
			public void onEmptyFail() {
				Log.i(TAG, "---->onDeleteCharFail onEmptyFail " + 27);
				//Log.i("whw", "清空失败！");
				mMessage.what = UIHelper.FINGERPRINT_EMPTY_FAIL;
			}
		});
	}

	// 录入指纹
	private void registerFingerprintSync() {
		mRetryCount = 0;
		if (mRegisterFingerprint != null) {
			mRegisterFingerprint.FP_GetImage();
		}
	}

	/* (non-Javadoc)
	 * @see com.jason.fingerprint.logic.Logic#destory()
	 */
	@Override
	public void destory() {
		// TODO Auto-generated method stub
		isRunning = false;
		mRetryCount= 0;

	}

}
